<?php

namespace StudyBuddy\Forms;

use \StudyBuddy\LoginForm;
use \StudyBuddy\String\HTMLString;

/**
 * Class responsible for processing the
 * Answer form as well as rendering the ask form
 *
 */
class Answerform extends Form {

    /**
     * Name of form template file
     * The name of actual template should be
     * set in sub-class
     *
     * @var string
     */
    protected $template = 'tplFormanswer';

    /**
     * Generates the html of the answer form
     * OR an html block explaining why form is not
     * available (like when question is marked as closed)
     * and also setting the 'mustLogin' related elements
     * if user not logged in
     *
     * @param Question $oQuestion
     *
     * @return string html of form or message block
     */
    public function getAnswerForm(\StudyBuddy\Question $oQuestion) {

        d('cp');


        /**
         * A custom observer may cancel the onBeforeAnswerform event
         * in order to NOT display the form
         * For example, a filter (implemented as an observer)
         * may decide not to show an answer form to users
         * from rough states based on GeoLocation by IP
         * For example - don't show answer form in China
         * OR NOT show answer form based on Viewer object if viewer
         * has been flagged as spammer
         */
        $oNotification = $this->oRegistry->Dispatcher->post($oQuestion, 'onBeforeAnswerForm');
        d('cp');
        if ($oNotification->isNotificationCancelled()) {
            d('onBeforeAnswerform cancelled by observer');

            return '';
        }
        d('cp');
        /**
         * If questions is closed then
         * instead of answer form return
         * the div with "closed" message
         */
        if (false !== $aClosed = $oQuestion->isClosed()) {

            return \tplClosedby::parse($aClosed);
        }
        d('cp');


        $formTitle = (0 === $oQuestion['i_ans']) ? $this->Tr['Be the first to answer this question'] : $this->Tr['Your answer'];

        $this->setVar('title', $formTitle);
        $this->setVar('qid', $oQuestion['_id']);
        $this->setVar('submit', $this->Tr['Submit answer']);
        $this->setVar('preview', $this->Tr['Preview']);

        if ($this->oRegistry->Viewer->isGuest()) {
            $this->template = 'tplFormanswerguest';
            d('cp');
            $this->qbody = $this->_('Please login to post your answer');
            $this->com_hand = ' com_hand';
            $this->readonly = 'readonly="readonly"';
            $this->disabled = ' disabled="disabled"';

            $oQuickReg = new \StudyBuddy\RegBlockQuickReg($this->oRegistry);
            d('cp');
            $socialButtons = LoginForm::makeSocialButtons($this->oRegistry);
            /**
             * @todo Translate string
             */
            if (!empty($socialButtons)) {
                $this->connectBlock = '<div class="com_connect"><h3>Join with account you already have</h3>' . $socialButtons . '</div>';
            }
        }
        d('cp');
        $form = $this->getForm();

        return $form;
    }

    /**
     * Enforce config values MIN_ANSWER_CHARS
     * and MIN_ANSWER_WORDS
     * 
     * @todo Translate strings of error messages
     *
     * (non-PHPdoc)
     * @see StudyBuddy\Forms.Form::doValidate()
     *
     * @return object $this
     */
    protected function doValidate() {

        $minChars = $this->oRegistry->Ini->MIN_ANSWER_CHARS;
        $minWords = $this->oRegistry->Ini->MIN_ANSWER_WORDS;
        $body = $this->oRegistry->Request->getUTF8('qbody');
        $oHtmlString = HTMLString::factory($body);
        $wordCount = $oHtmlString->getWordsCount();
        $len = $oHtmlString->length();

        if ($len < $minChars) {
            $this->setError('qbody', 'Answer must contain at least ' . $minChars . ' letters');
        }

        if ($wordCount < $minWords) {
            $this->setError('qbody', 'Answer must contain at least ' . $minWords . ' words');
        }

        return $this;
    }

}
